// 全局配置文件
// 用于管理API域名、SDK路径等配置
// 支持不同环境的快速切换

const globalConfig = {
  production: {
    API_BASE_URL: 'https://api.xapptool.cn',
    SDK_BASE_URL: 'https://sdk.xapptool.cn',
    MERCHANT_URL: 'https://merchant.xapptool.cn',
    LANDING_URL: 'https://landing.xapptool.cn'
  },
  development: {
    API_BASE_URL: 'http://localhost:8787',
    SDK_BASE_URL: 'http://localhost:8787',
    MERCHANT_URL: 'http://localhost:3000',
    LANDING_URL: 'http://localhost:3000'
  }
};

// 获取当前环境
const env = import.meta.env.MODE || 'production';

// 导出当前环境的配置
export const config = globalConfig[env] || globalConfig.production;

// SDK路径配置
export const sdkPaths = {
  web: `${config.SDK_BASE_URL}/web/xapptool.js`,
  uniapp: `${config.SDK_BASE_URL}/uniapp/xapptool.js`,
  react: `${config.SDK_BASE_URL}/react/xapptool.js`,
  android: `${config.SDK_BASE_URL}/android/XappToolAndroid.java`,
  ios: `${config.SDK_BASE_URL}/ios/XappTooliOS.swift`
};

// API端点配置
export const apiEndpoints = {
  auth: {
    login: `${config.API_BASE_URL}/auth/login`,
    register: `${config.API_BASE_URL}/auth/register`,
    logout: `${config.API_BASE_URL}/auth/logout`,
    profile: `${config.API_BASE_URL}/auth/profile`
  },
  apps: {
    list: `${config.API_BASE_URL}/apps/list`,
    create: `${config.API_BASE_URL}/apps/create`,
    update: `${config.API_BASE_URL}/apps/update`,
    delete: `${config.API_BASE_URL}/apps/delete`,
    detail: `${config.API_BASE_URL}/apps/detail`
  },
  stats: {
    overview: `${config.API_BASE_URL}/stats/overview`,
    detail: `${config.API_BASE_URL}/stats/detail`,
    export: `${config.API_BASE_URL}/stats/export`
  },
  tracking: {
    device: `${config.API_BASE_URL}/device/track`,
    event: `${config.API_BASE_URL}/device/event`,
    attribution: `${config.API_BASE_URL}/device/attribution`,
    landing: `${config.API_BASE_URL}/landing/track`
  }
};

// 默认分享配置
export const shareConfig = {
  title: '免邀请码安装 - XappTool',
  description: '专业的免邀请码安装追踪解决方案',
  image: `${config.MERCHANT_URL}/assets/share-image.png`,
  url: config.MERCHANT_URL
};

// 应用商店配置
export const appStores = {
  appstore: {
    name: 'App Store',
    placeholder: 'https://apps.apple.com/app/idYOUR_APP_ID',
    help: '在App Store中找到您的应用，复制URL中的ID数字'
  },
  googleplay: {
    name: 'Google Play',
    placeholder: 'https://play.google.com/store/apps/details?id=YOUR_PACKAGE_NAME',
    help: '在Google Play中找到您的应用，复制完整的包名'
  },
  huawei: {
    name: '华为应用市场',
    placeholder: 'https://appgallery.huawei.com/app/CYOUR_APP_ID',
    help: '在华为应用市场中找到您的应用，复制URL'
  },
  xiaomi: {
    name: '小米应用商店',
    placeholder: 'https://app.mi.com/details?id=YOUR_PACKAGE_NAME',
    help: '在小米应用商店中找到您的应用，复制包名'
  },
  oppo: {
    name: 'OPPO软件商店',
    placeholder: 'https://store.oppomobile.com/product/YOUR_PRODUCT_ID.html',
    help: '在OPPO软件商店中找到您的应用，复制产品ID'
  },
  vivo: {
    name: 'vivo应用商店',
    placeholder: 'https://dev.vivo.com.cn/product/productinfo?id=YOUR_PRODUCT_ID',
    help: '在vivo应用商店中找到您的应用，复制产品ID'
  },
  honour: {
    name: '荣耀应用市场',
    placeholder: 'https://ag.hihonor.com/app/CYOUR_APP_ID',
    help: '在荣耀应用市场中找到您的应用，复制URL'
  },
  yyb: {
    name: '应用宝',
    placeholder: 'https://sj.qq.com/myapp/detail.htm?apkName=YOUR_PACKAGE_NAME',
    help: '在应用宝中找到您的应用，复制包名'
  }
};

// 获取配置的工具函数
export const getConfig = () => config;
export const getApiUrl = (endpoint) => {
  const parts = endpoint.split('.');
  let current = apiEndpoints;
  for (const part of parts) {
    current = current[part];
    if (!current) return null;
  }
  return current;
};
export const getSdkUrl = (type) => sdkPaths[type];
export const getAppStore = (store) => appStores[store];

export default {
  config,
  sdkPaths,
  apiEndpoints,
  shareConfig,
  appStores,
  getConfig,
  getApiUrl,
  getSdkUrl,
  getAppStore
};
